package vista;

import controlador.AnalizadorControlador;
import javax.swing.JOptionPane;
import model.Archivo;

public class VistaAnalizador extends javax.swing.JFrame {

    /**
     * Creates new form VistaAnalizador
     */
    public VistaAnalizador() {
        initComponents();
        yucalexControl = new AnalizadorControlador();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        htmlTextArea = new javax.swing.JTextArea();
        jScrollPane1 = new javax.swing.JScrollPane();
        lexTextArea = new javax.swing.JTextArea();
        abrirHTMLBtn = new javax.swing.JButton();
        generarAnalisisBtn = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        guardarLexicoBtn = new javax.swing.JButton();
        aboutBtn = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        abrirHTMLMenuItem = new javax.swing.JMenuItem();
        abrirLexMenuItem = new javax.swing.JMenuItem();
        guardarHTMLMenuItem = new javax.swing.JMenuItem();
        guardarLEXMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("YucaLexer");
        setBackground(new java.awt.Color(0, 0, 0));
        setResizable(false);

        htmlTextArea.setColumns(20);
        htmlTextArea.setRows(5);
        htmlTextArea.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        jScrollPane2.setViewportView(htmlTextArea);

        lexTextArea.setEditable(false);
        lexTextArea.setColumns(20);
        lexTextArea.setRows(5);
        jScrollPane1.setViewportView(lexTextArea);

        abrirHTMLBtn.setText("Abrir HTML");
        abrirHTMLBtn.setToolTipText("Abrir un archivo Html para analizar");
        abrirHTMLBtn.setName("abrirHTMLBtn"); // NOI18N
        abrirHTMLBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abrirHTMLBtnActionPerformed(evt);
            }
        });

        generarAnalisisBtn.setText("Generar Analisis");
        generarAnalisisBtn.setToolTipText("Iniciar el analisis lexico");
        generarAnalisisBtn.setName("generarAnalisisBtn"); // NOI18N
        generarAnalisisBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generarAnalisisBtnActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/logo.png"))); // NOI18N
        jLabel1.setName("iconLb"); // NOI18N

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/text3230-1.png"))); // NOI18N
        jLabel2.setName("logoLb"); // NOI18N

        guardarLexicoBtn.setText("Guardad Lex");
        guardarLexicoBtn.setToolTipText("Genera un archivo con la salida del analizador lexico");
        guardarLexicoBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarLexicoBtnActionPerformed(evt);
            }
        });

        aboutBtn.setText("About Us");
        aboutBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutBtnActionPerformed(evt);
            }
        });

        jLabel3.setText("Agregue o Edite Codigo HTML, Guarde y Genere el Analisis");

        jMenu1.setText("Archivo");

        abrirHTMLMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        abrirHTMLMenuItem.setText("Abrir HTML");
        abrirHTMLMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abrirHTMLMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(abrirHTMLMenuItem);

        abrirLexMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        abrirLexMenuItem.setText("Abrir Archivo Lexico");
        abrirLexMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abrirLexMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(abrirLexMenuItem);

        guardarHTMLMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G, java.awt.event.InputEvent.CTRL_MASK));
        guardarHTMLMenuItem.setText("Guardar HTML");
        guardarHTMLMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarHTMLMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(guardarHTMLMenuItem);

        guardarLEXMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        guardarLEXMenuItem.setText("Guardar Archivo lex");
        guardarLEXMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarLEXMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(guardarLEXMenuItem);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 440, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(abrirHTMLBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(generarAnalisisBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(guardarLexicoBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 440, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(aboutBtn))
                .addGap(21, 21, 21))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(abrirHTMLBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(37, 37, 37)
                        .addComponent(generarAnalisisBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(37, 37, 37)
                        .addComponent(guardarLexicoBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(aboutBtn))
                        .addGap(2, 2, 2)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 431, Short.MAX_VALUE)
                            .addComponent(jScrollPane2))))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void abrirHTMLBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abrirHTMLBtnActionPerformed
        try {
            yucalexControl.abrirArchivo(htmlTextArea, Archivo.Ext_HTML);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e);
        }
    }//GEN-LAST:event_abrirHTMLBtnActionPerformed

    private void generarAnalisisBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generarAnalisisBtnActionPerformed
        if(!(htmlTextArea.getText().equals(""))){
             try {
                yucalexControl.analizar(lexTextArea, htmlTextArea.getText());
             } catch (Exception e) {
                 JOptionPane.showMessageDialog(this, e);
             }
        }else{
            JOptionPane.showMessageDialog(this, "Abra un archivo HTML primero");
        }
    }//GEN-LAST:event_generarAnalisisBtnActionPerformed

    private void aboutBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutBtnActionPerformed
        // TODO add your handling code here:
        Licencia lic = new Licencia(this, false);
        lic.setVisible(true);
    }//GEN-LAST:event_aboutBtnActionPerformed

    private void guardarLexicoBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarLexicoBtnActionPerformed
        try {
            yucalexControl.guardarArchivo(Archivo.Ext_LEX, lexTextArea.getText());
            JOptionPane.showMessageDialog(this, "Archivo Guardado", null, JOptionPane.OK_OPTION);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e);
        }
    }//GEN-LAST:event_guardarLexicoBtnActionPerformed

    private void abrirHTMLMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abrirHTMLMenuItemActionPerformed
        // TODO add your handling code here:
        try {
            yucalexControl.abrirArchivo(htmlTextArea, Archivo.Ext_HTML);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e);
        }
    }//GEN-LAST:event_abrirHTMLMenuItemActionPerformed

    private void guardarHTMLMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarHTMLMenuItemActionPerformed
        // TODO add your handling code here:
        try {
            yucalexControl.guardarArchivo(Archivo.Ext_HTML, htmlTextArea.getText());
            JOptionPane.showMessageDialog(this, "Archivo Guardado", null, JOptionPane.OK_OPTION);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e);
        }
    }//GEN-LAST:event_guardarHTMLMenuItemActionPerformed

    private void guardarLEXMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarLEXMenuItemActionPerformed
        // TODO add your handling code here:
        try {
            yucalexControl.guardarArchivo(Archivo.Ext_LEX, lexTextArea.getText());
            JOptionPane.showMessageDialog(this, "Archivo Guardado", null, JOptionPane.OK_OPTION);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e);
        }
    }//GEN-LAST:event_guardarLEXMenuItemActionPerformed

    private void abrirLexMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abrirLexMenuItemActionPerformed
        // TODO add your handling code here:
        try {
            yucalexControl.abrirArchivo(lexTextArea, Archivo.Ext_LEX);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e);
        }
    }//GEN-LAST:event_abrirLexMenuItemActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VistaAnalizador.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VistaAnalizador.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VistaAnalizador.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VistaAnalizador.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new VistaAnalizador().setVisible(true);
            }
        });
    }
    //Variables propias
    private AnalizadorControlador yucalexControl;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aboutBtn;
    private javax.swing.JButton abrirHTMLBtn;
    private javax.swing.JMenuItem abrirHTMLMenuItem;
    private javax.swing.JMenuItem abrirLexMenuItem;
    private javax.swing.JButton generarAnalisisBtn;
    private javax.swing.JMenuItem guardarHTMLMenuItem;
    private javax.swing.JMenuItem guardarLEXMenuItem;
    private javax.swing.JButton guardarLexicoBtn;
    private javax.swing.JTextArea htmlTextArea;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea lexTextArea;
    // End of variables declaration//GEN-END:variables
}
